#include "cpu/exec/template-start.h"

//0x55 push
//关于寄存器编号请参考i386手册
enum { eax = 0, 
	ecx = 1, 
	edx = 2, 
	ebx = 3, 
	esp = 4, 
	ebp = 5, 
	esi = 6, 
	edi = 7 };

make_helper(push_r_v){
    //解析参数
    int len = concat(decode_r_, SUFFIX)(eip + 1);
    //将寄存器的值压栈
    cpu.esp -= DATA_BYTE;
    Log ("op_src->reg = %d", op_src->reg);
    //提取寄存器的值
    switch (op_src->reg){
        case eax: op_src->val = cpu.eax; break;
        case ecx: op_src->val = cpu.ecx; break;
        case edx: op_src->val = cpu.edx; break;
        case ebx: op_src->val = cpu.ebx; break;
        case esp: op_src->val = cpu.esp; break;
        case ebp: op_src->val = cpu.ebp; break;
        case esi: op_src->val = cpu.esi; break;
        case edi: op_src->val = cpu.edi; break;
        default: Assert(0, "push_r_v error");
    }
    swaddr_write(cpu.esp, DATA_BYTE, op_src->val);
    
    print_asm("push %s", op_src->str);
    return len + 1;
}


#include "cpu/exec/template-end.h"


#include "cpu/exec/template-start.h"

//0x5d pop

make_helper(pop_r_v){
    //解析参数
    int len = concat(decode_r_, SUFFIX)(eip + 1);
    //将栈顶的值弹出到寄存器
    // Log ("DATA_BYTE = %d", DATA_BYTE);
    unsigned int temp = swaddr_read(cpu.esp, DATA_BYTE);
    //提取寄存器的值
    switch (op_src->reg){
        case eax: cpu.eax = temp; break;
        case ecx: cpu.ecx = temp; break;
        case edx: cpu.edx = temp; break;
        case ebx: cpu.ebx = temp; break;
        case esp: cpu.esp = temp; break;
        case ebp: cpu.ebp = temp; break;
        case esi: cpu.esi = temp; break;
        case edi: cpu.edi = temp; break;
        default: Assert(0, "pop_r_v error");
    }
    
    cpu.esp += DATA_BYTE;
    print_asm("pop %s", op_src->str);
    return len + 1;
}

#include "cpu/exec/template-end.h"